<?php
namespace app\cms\controller;
use framework\ext\Check;
/* *
 * 栏目管理
 * */
class ChannelController extends \app\base\controller\AdminController{

    //分类首页
    public function index(){
        $act = form('get.act');
        if($act == 'del'){
            $fid = form('get.id',0,intval);
            $result =  model('channel')->_delete($fid);
            if(!$result){
                $this->jsonMsg('删除成功',1);
            }else{
                $this->jsonMsg($result);
            }
        }else{
            $page = form('get.page',0,intval);
            $this->pid = form('get.id',0,intval);  //当前ID
            $this->path = model('channel')->path($this->pid,'channel/index');
            $this->list = model('channel')->pager($page,20)->_select($this->pid,$this->website_id);
            $this->pager = getPage(model('channel')->pager,array('id'=>$this->pid));
            $this->display();
        }
    }

    //添加分类
    public function add(){
        if($this->isPost()){
            $msg = Check::rule(array(check::must($_POST['mid']),'绑定模型必须选择'),array(check::must($_POST['name']),'栏目名称必须填写'));
            if(true !== $msg){
                $this->jsonMsg($msg);
            }
            $data = array();
            $data = form('post',array('fid'));
            if($data['type'] == 2){
                if(!$data['url']){
                    $this->jsonMsg('栏目URL没有填写');
                }
            }
            //获取根ID开始
            if($data['pid'] == 0){
                $data['rootid'] = 0;
                $data['website_id'] = $this->website_id?$this->website_id:form('post.website_id',0,intval);
            }else{
                $root = model('channel')->_find(array('id'=>$data['pid']),'id,pid,website_id,rootid');
                if($root['pid'] == 0){
                    $data['rootid'] = $root['id'];
                }else{
                    $data['rootid'] = $root['rootid'];
                }
                $data['website_id'] = $root['website_id'];
            }
            //插入模型数据
            $data['uptime'] = time();
            $info = model('channel')->_update($data);
            if($info){
                $this->jsonMsg('栏目添加成功',1,url('channel/index',array('id'=>$data['pid'])));
            }else{
                $this->jsonMsg('栏目添加失败');
            }

        }else{
            $this->pid = form('get.id',0,intval);
            if($this->pid){
                $this->list = model('channel')->_find(array('id' => $this->pid),'mid');
            }
            $this->lists = model('module')->_select(array('locks' => 0),'id desc','name,id,type'); 
            $this->path = model('channel')->path($this->pid,'channel/index'); //路径
            $this->display();
        }   
    }
    //编辑分类
    public function edit(){
        if($this->isPost()){
            $msg = Check::rule(array(check::must($_POST['name']),'栏目名称必须填写'));
            if(true !== $msg){
                $this->jsonMsg($msg);
            }
            $data = array();
            $data = form('post',array('fid','pid'));
            $data['uptime'] = time();
            if($data['type'] == 2){
                if(!$data['url']){
                    $this->jsonMsg('栏目URL没有填写');
                }
            }
            $data['website_id'] = $this->website_id?$this->website_id:form('post.website_id',0,intval);
            $fid = form('post.fid',0,intval);
            $pid = form('post.pid',0,intval);;
            $mid = form('post.exts_mid',0,intval);
            $info = model('channel')->editClass($fid,$mid,$data); //插入模型数据
            if($info){
                $this->jsonMsg('栏目编辑成功',1,url('channel/index',array('id'=>$pid))); 
            }else{
                $this->jsonMsg('栏目编辑失败');
            }
        }else{
            $this->fid = form('get.id',0,intval);
            $this->info = model('channel')->_find(array('id' => $this->fid));
            $this->path = model('channel')->path($this->fid,'channel/index');
            if(intval($this->info['exts_mid'])){
                $input_value = model('channel')->editClassExt($this->fid,$this->info['exts_mid']);
                $this->module = model('field')->fromInput($this->info['exts_mid'],TRUE,$input_value); 
            }
            $this->display(); 
        }
    } 

    //字段排序
    public function uporter(){
        $condition['id'] = form('post.id',0,intval);
        $data['neworder'] = form('post.sorter',0,intval);
        $info = model('channel')->where($condition)->data($data)->update();
        if($info){
            $this->jsonMsg('自定义排序成功',1);
        }else{
            $this->jsonMsg('自定义排序失败');
        }
    }


    //站内链接
    public function linkurl(){
        $this->frameid = form('get.id');
        $this->types = form('get.types');
        $this->pid = form('get.pid',0,intval);
        switch ($this->types) {
            case 1:
                $this->path = model('channel')->path($this->pid);
                $this->channel = model('channel')->_select($this->pid,$this->website_id);
                break;
            case 2:
                $this->diypage = model('diypage')->_select($this->website_id);
                break;
            case 3:
                $this->path = model('special')->path($this->pid);
                $this->special = model('special')->_select($this->pid,$this->website_id);
                break;
            default:
                break;
        }
        $this->display(); 
    }
}